package com.study.rabbitmq.advance;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 消费者：
 * 消费模式 1.拉 pull 2.push 推
 * 可靠性 ack
 */
public class PushRecv {
    private final static String QUEUE_NAME = "hello";
    private final static String EXCHANGE_NAME = "";
    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.20.68");
        factory.setPort(5672);
        factory.setUsername("leke");
        factory.setPassword("leke@@@");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body);
                System.out.println(" [x] Received '" + message + "'");

              //  channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };

        channel.basicConsume(QUEUE_NAME,true,consumer);


    }
}
